<?php

/**
 * @name UrlManager
 * @author Roman Metlinskyi <r.metlinskyi@gmail.com>
 */
class UrlManager extends Object
{
  private $baseUrl;

  public $controllerName;
  public $actionName;
  public $arguments;

  /*
   * region: ISingleton implement
   */
  private static $_urlManager;

  private function __construct()
  {
    $url = My::System()->Web->HttpRequest->Url();

    $this->baseUrl = $request->PathInfo;

    $uri = (strcmp($this->baseUrl, '/') == 0 )
            ? substr($request->RequestUrl, 1)
            : str_replace($this->baseUrl, '', $request->RequestUrl);


    if (isset($url->Segments[0]))
      $this->controllerName = $url->Segments[0];

    if (isset($url->Segments[1]))
      $this->actionName = $url->Segments[1];

    $this->arguments = $url->Params;

  }

  private function __clone()
  {

  }

  public static function UrlManager()
  {
    return (!isset(self::$_urlManager)) ? (self::$_urlManager = new self()) : self::$_urlManager;
  }

  /*
   * region: Properties [GET]
   */

  public function Resolve($controllerName, $actionName = null, $params = null)
  {
    if ($actionName==null
            &&
        strcasecmp($controllerName, My::System()->ConfigManager->Mvc['controller']['default']) == 0
    ){
      return $this->baseUrl;
    }
    
    if ($actionName)
      $actionName = "/$actionName";

    return "{$this->baseUrl}{$controllerName}{$actionName}";
  }

}
